# 向量的实现
import math


class Vector(object):
    # 向量初始化
    def __init__(self, components=[]):
        self.__component = list(components)

    def set(self, components):
        # 改变向量内的数组
        if len(components) > 0:
            self.__component = list(components)
        else:
            raise Exception('Please give any vector')

    # 返回向量的字符形式
    def __str__(self):
        return "(" + ",".join(map(str, self.__component)) + ")"

    # 返回向量的某个元素
    def component(self, i):
        if type(i) is int and -len(self.__component) <= i < len(self.__component):
            return self.__components[i]
        else:
            raise Exception("Index out of range")

    # 返回向量的长度
    def __len__(self):
        return len(self.__components)

    # 返回向量的欧几里得长度
    def eulid_length(self):
        summe = 0
        for c in self.__components:
            summe += c**2
        return math.sqrt(summe)

    # 改变向量指定位置的元素
    def change_component(self, position, value):
        assert (-len(self.__components) <= position < len(self.__components))
        self.__components[position] = value

    # 向量的加法
    def __add__(self, other):
        size = len(self)
        if size == len(other):
            result = [self.__components[i] +
                      other.component(i) for i in range(size)]
            return Vector(result)
        else:
            raise Exception("Must have same size")

    # 向量的减法
    def __sub__(self, other):
        size = len(self)
        if size == len(other):
            result = [self.__components[i] -
                      other.component(i) for i in range(size)]
            return result
        else:
            raise Exception("Must have same size")

    # 向量的数乘
    def __mul__(self, other):
        if isinstance(other, float) or isinstance(other, int):
            answer = [c * other for c in self.__components]
            return answer
        elif isinstance(other, Vector) and (len(self) == len(other)):
            size = len(self)
            summe = 0
            for i in range(size):
                summe += self.__components[i] * other.component(i)
            return summe
        else:
            raise Exception("Invalid operand")

    def zero_vector(dimension):
        assert(isinstance(dimension, int))
        return Vector([0 * dimension])

    def unit_basis_vector(dimension, pos):  # 单位基向量的实现
        assert (isinstance(dimension, int) and (isinstance(pos, int)))
        ans = [0] * dimension
        ans[pos] = 1
        return Vector(ans)